<template>
  <div class="app-container">
    <header>
      欢迎注册
    </header>

    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="厂家名称" prop="shopName">
        <el-input v-model.trim="form.shopName"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="phone">
        <el-input v-model.trim="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          type="password"
          v-model="form.pwd"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPwd">
        <el-input
          type="password"
          autocomplete="off"
          v-model="form.checkPwd"
        ></el-input>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="onSubmit">立即注册</el-button>
  </div>
</template>

<script>
import { addShop } from "@/api";
import { validPhone } from "@/utils/validate";
export default {
  name: "StoreRegistered",
  data() {
    return {
      //表单信息--------------------------------------------
      form: {
        shopName: "",
        phone: "",
        pwd: "",
        checkPwd: ""
      },
      //校验--------------------------------------------
      rules: {
        phone: [
          { required: true, validator: this.validatePhone, trigger: "blur" }
        ],
        shopName: {
          required: true,
          message: "请输入厂家名称",
          trigger: "blur"
        },
        pwd: [
          { required: true, validator: this.validatePass, trigger: "blur" }
        ],
        checkPwd: [
          { required: true, validator: this.validatePass2, trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    //校验密码--------------------------------------------
    validatePass(rule, value, callback) {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.pwd !== "") {
          this.$refs.form.validateField("checkPwd");
        }
        callback();
      }
    },
    //校验密码--------------------------------------------
    validatePass2(rule, value, callback) {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    },
    //校验手机号--------------------------------------------
    validatePhone(rule, value, callback) {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    },

    //校验结果--------------------------------------------
    validate() {
      let flag;
      this.$refs.form.validate(valid => {
        flag = valid;
      });
      return flag;
    },

    onSubmit() {
      if (!this.validate()) return;
      const data = {
        ...this.form
      };
      addShop(data).then(res => {
        if (res.state === 100) {
          this.$message({ message: res.msg, type: "success" });
          this.form = {
            shopName: "",
            phone: "",
            pwd: "",
            checkPwd: ""
          };
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  header {
    text-align: center;
    font-size: 36px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #0090ff;
    line-height: 80px;
  }

  .el-form {
    width: 399px;
  }
}
</style>
